<?php
/**
 * Created by PhpStorm.
 * User: wx
 * Date: 2018/7/30
 * Time: 20:47
 */

namespace wechat\core;

use wechat\Wx;

class JsSdk extends Wx
{
    public function makeSign(){
        //设置参与生成签名的参数
        $noncestr = $this->makeStr();
        $jsapi_ticket = $this->getJsapiTicket();
        $timestamp = time();
        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $arr = [
            'noncestr=' . $noncestr,
            'jsapi_ticket=' . $jsapi_ticket,
            'timestamp=' . $timestamp,
            'url=' . $url,
        ];
        //字典化排序
        sort($arr,SORT_STRING);
        //将$arr数组切割成字符串
        $str = implode($arr,'&');
        //对$str进行sha1加密,生成签名
        $sign = sha1($str);
        //返回之前,组合需要使用的数据的数组
        $array = [
            'sign' => $sign,
            'timestamp' => $timestamp,
            'nonceStr' => $noncestr,
            'appId' => 'wx33062c92820bd0f0',
            'url' => $url
        ];
        //var_export($array);die;
        //$array = json_encode($array);
        //echo $array;die;
        //将数组返回
        return $array;
    }

    /**
     *
     * 生成随机字符串
     *
     * @return string
     */
    public function makeStr(){
        $seed = '1qwer2tyu3uiop4asdf5ghj7zcxvn8mkl';
        //定义一个字符串,用来接收随机字符串
        $str = '';
        for ($i = 0; $i < 8; $i++){
            $num = mt_rand(0,strlen($seed) - 1);
            $str .= $seed[$num];
        }
        return $str;
    }

}